package Control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import javax.servlet.http.HttpSession;

import Bean.BeanCinema;
import Model.Cinema;
import DAO.DAOCinema;
/**
 * Servlet implementation class ControladorCinema
 */
public class ControladorCinema extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, Exception {
		
		response.setContentType("text/html;charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
    
        try {
           String opcao = request.getParameter("opcao");
           HttpSession session = request.getSession();
          // if (session == null) {
        	//   response.sendRedirect("../AcessoAoSistema/login.jsp"); 
           //}else{
        	   if (opcao != null) {
        		   if (opcao.equals("salvar")){

        			   if (request.getParameter("codigo") != null){
        			   if (request.getParameter("codigo") == "0") 
        				   this.incluirCinema(request,response);
        			   else
        				   this.atualizarCinema(request,response);
        			   }
        			   this.listarFiltro(request, response);
        		   } else if (opcao.equals("listarTodos")){
        			   this.listarTodosCinemas(request,response);
        			   
        		   } else if (opcao.equals("pesquisar")){
        			   this.listarFiltro(request, response);
        		   }else if (opcao.equals("editar")){
        			   this.editarCinema(request, response);
        		   }else if (opcao.equals("excluir")){
        			   this.excluirCinema(request, response);
        			   this.listarFiltro(request, response);
        		   }
        		   else response.sendRedirect("../paginaPrincipal.jsp");
        		   
        	   }	   
           //}
        } catch(Exception e ){
            System.out.println(e.getLocalizedMessage());
        } finally {
            out.close();
        }
    }
    
    private void incluirCinema(HttpServletRequest request,HttpServletResponse response) throws Exception{
    	DAOCinema dao = new DAOCinema();
    	Cinema Cinema = new Cinema(request.getParameter("nome"),request.getParameter("cnpj"),request.getParameter("endereco"),request.getParameter("cidade"),request.getParameter("estado"),request.getParameter("telefone"),request.getParameter("email"));
    	if (!dao.salvar(Cinema)) {
    		response.sendRedirect("AcessoAoSistema/login.jsp");	
    	}	
    }
    
    private void atualizarCinema(HttpServletRequest request,HttpServletResponse response) throws Exception{
    	DAOCinema dao = new DAOCinema();
    	Cinema Cinema = new Cinema(request.getParameter("nome"),request.getParameter("cnpj"),request.getParameter("endereco"),request.getParameter("cidade"),request.getParameter("estado"),request.getParameter("telefone"),request.getParameter("email"));
    	if (!dao.atualizar(Cinema)) {
    		response.sendRedirect("AcessoAoSistema/login.jsp");	
    	}	
    }
    
    private void listarTodosCinemas(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	DAOCinema dao = new DAOCinema();
    	BeanCinema beanConsulta = new BeanCinema();
    	beanConsulta.setCinemas(dao.listar());
    	request.setAttribute("beanCinemaConsulta",beanConsulta);
    	RequestDispatcher dispatcher = request.getRequestDispatcher("Cinemas/gerenciarCinemas.jsp");	
    	dispatcher.forward(request, response);
    }
    	
    private void listarFiltro(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	DAOCinema dao = new DAOCinema();
    	BeanCinema beanConsulta = new BeanCinema();
    	int idadeMinima = 0;
    	String filtroNome="";
    	if (request.getParameter("filtroNomeCinema") != null) 
    		filtroNome = request.getParameter("filtroNomeCinema");
    	ArrayList<Cinema> listarFiltroCinema = dao.listarFiltroCinema(filtroNome);
    	beanConsulta.setCinemas(listarFiltroCinema);
    	request.setAttribute("beanCinemaConsulta",beanConsulta);
    	RequestDispatcher dispatcher = request.getRequestDispatcher("Cinemas/gerenciarCinemas.jsp");	
    	dispatcher.forward(request, response);
    }
    
    private void editarCinema(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	DAOCinema dao = new DAOCinema();
    	String cnpj = request.getParameter("cnpj");
    	Cinema cinema = dao.procurar(cnpj);
    	BeanCinema bean = new BeanCinema(cinema.getNome(),cinema.getCnpj(),cinema.getEndereco(),cinema.getCidade(),cinema.getEstado(),cinema.getTelefone(),cinema.getEmail(),null);
    	request.setAttribute("beanCinema",bean);
    	RequestDispatcher dispatcher = request.getRequestDispatcher("Cinemas/gerenciarCinemas.jsp");	
    	dispatcher.forward(request, response);
    }
    
    private void excluirCinema(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	DAOCinema dao = new DAOCinema();
    	String cnpj = request.getParameter("cnpj");
    	dao.excluir(cnpj);
    }
    

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception e ) {
            Logger.getLogger(ControladorCinema.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception e ) {
            Logger.getLogger(ControladorCinema.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
